"""Compute and plot distributions of  depth, size and number of cascades per videos."""
import numpy as np
import sys
import os
import math
import pylab as plt
import networkx as nx
from params import *

cascade_depth = {}
cascade_user = {}
cascade_per_video = {}
threshold = 0
stats = []
videos = set()
cascade_per_user = {}
tracefile = os.path.join(WORKDIR, 'geocascades', 'traces',
    'final_cascades_2days.txt')
for line in open(tracefile):
    hash,cascade = eval(line)
    videos.add(hash)
    l = len(cascade)
    seed = cascade[0][2]
    if not seed in cascade_per_user:
        cascade_per_user[seed] = 0
    cascade_per_user[seed] += 1

    if l >= threshold:
        depth = max(x[4] for x in cascade)
        users = len(cascade)+1

        if not hash in cascade_per_video:
            cascade_per_video[hash] = 0
        cascade_per_video[hash] += 1

        if not depth in cascade_depth:
            cascade_depth[depth] = 0
        cascade_depth[depth] += 1

        if not users in cascade_user:
            cascade_user[users] = 0
        cascade_user[users] += 1

print "videos ", len(videos)

#cascade_number = {}
#for hash in cascade_per_video:
#    c = cascade_per_video[hash]
#    if not c in cascade_number:
#        cascade_number[c] = 0
#    cascade_number[c] += 1
#
#x = sorted([k for k in cascade_number if k])
#y = [cascade_number[k] for k in x if k]
#plt.figure()
#plt.clf()
#plt.axes(FIG_AXES2)
#plt.loglog(x,y,'k.')
#plt.xlabel('k')
#plt.ylabel('Number of videos with k different cascades')
#plt.grid(True)
#plt.savefig('cascade_number.pdf')
#plt.close()

#cascade_number = {}
#for user in cascade_per_user:
#    c = cascade_per_user[user]
#    if not c in cascade_number:
#        cascade_number[c] = 0
#    cascade_number[c] += 1
#
#x = sorted([k for k in cascade_number if k])
#y = [cascade_number[k] for k in x if k]
#plt.figure()
#plt.clf()
#plt.axes(FIG_AXES2)
#plt.loglog(x,y,'k.')
#plt.xlabel('k')
#plt.ylabel('Number of users with k different cascades')
#plt.grid(True)
#plt.savefig('cascade_users_number.pdf')
#plt.close()


x = sorted([k for k in cascade_user if k])
y = [cascade_user[k] for k in x if k]
#for a,b in zip(x,y)[:10]:
#    print a,b
#plt.figure()
#plt.clf()
#plt.axes(FIG_AXES2)
#plt.loglog(x,y,'ko', mfc='None')
#plt.xlabel('Cascade size')
#plt.ylabel('Number of cascades')
#plt.grid(True)
#plt.savefig('cascade_size.pdf')
#plt.close()


c = [y[0]]
for i in range(1,len(y)):
    t = c[i-1]
    c.append(t+y[i])
tot = c[-1]
c = map(lambda i: 1-1.0*i/tot,c)

plt.figure()
plt.clf()
plt.axes(FIG_AXES2)
plt.loglog(x,c,'ko', mfc='None')
plt.xlabel('Cascade size')
plt.ylabel('CCDF')
plt.grid(True)
plt.savefig('cascade_size_cum.pdf')
plt.close()


#x = sorted([k for k in cascade_depth if k])
#y = [cascade_depth[k] for k in x if k]
#plt.figure()
#plt.clf()
#plt.axes(FIG_AXES2)
#plt.loglog(x,y,'k.')
#plt.xlabel('Cascade depth')
#plt.ylabel('Number of cascades')
#plt.grid(True)
#plt.savefig('cascade_depth.pdf')
#plt.close()
#
#c = [y[0]]
#for i in range(1,len(y)):
#    t = c[i-1]
#    c.append(t+y[i])
#tot = c[-1]
#c = map(lambda i: 1-1.0*i/tot,c)
#
#plt.figure()
#plt.clf()
#plt.axes(FIG_AXES2)
#plt.loglog(x,c,'k.')
#plt.xlabel('Cascade depth')
##plt.ylabel('Number of cascades')
#plt.grid(True)
#plt.savefig('cascade_depth_cum.pdf')
#plt.close()
#
